<template>
  <div class="occPicker" @click="showOcc">
    职业类别
    <span>
      {{occName}}
      <i class="iconfont icon-arrow_down arrow-right"></i>
    </span>
    <div class="occPickerContent" :class="{showOccPicker: showOccPicker}">
      <div class="occPickerModal" @click.stop="hideOcc"></div>
      <div class="occPickerSection">
        <div class="searchDiv">
          <mt-search v-model="searchValue" :show="true">
            <mt-cell
              :key="index"
              v-for="(item, index) in occArray"
              :title="item.description"
              @click.native.stop="setOcc(item.description, item.level)"
              :value="item.level">
            </mt-cell>
          </mt-search>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'occPicker',
  data () {
    return {
      occName: '第2类职业',
      occValue: '2',
      searchValue: '',
      occArray: [],
      occDefaultArray: [],
      showOccPicker: false
    }
  },
  mounted () {
    this.getOcc()
  },
  methods: {
    getOcc () {
      axios.get('../static/occ.json').then(data => {
        for (let item of data.data) {
          if (Number(item.level) > 6) {
            item.level = '6'
          }
        }
        this.occDefaultArray = data.data
        this.occArray = data.data
      })
    },
    setOcc (des, val) {
      this.$emit('getOcc', des, val)
      this.occName = `第${val}类职业`
      this.showOccPicker = false
    },
    showOcc () {
      this.showOccPicker = true
    },
    hideOcc () {
      this.showOccPicker = false
    }
  },
  watch: {
    searchValue (val) {
      this.occArray = []
      for (let item of this.occDefaultArray) {
        if (item.description.indexOf(val) >= 0) {
          this.occArray.push(item)
        }
      }
    }
  }
}
</script>

<style scoped>
  .occPicker {
    padding: 5px 15px;
    position: relative;
    color: #535353;
    line-height: 27px;
    font-size: 14px;
    text-align: left;
  }
  .occPicker>span {
    height: 25px;
    border: 1px solid #c9c9c9;
    text-align: left;
    line-height: 27px;
    position: absolute;
    right: 15px;
    padding: 0 18px 0 5px;
    font-size: 14px;
    max-width: 200px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    border-radius: 3px;
  }
  .arrow-right {
    position: absolute;
    right: 5px;
    top: 0px;
    color: #8a8a8a;
    font-size: 12px;
    line-height: 25px;
  }
  .occPickerContent {
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    top: 0;
    left: 100%;
    z-index: 999;
    overflow: hidden;
    transition: left .2s linear;
  }
  .occPickerContent.showOccPicker {
    left: 0;
  }
  .occPickerModal {
    width: 30%;
    height: 100%;
    background: none;
    float: left;
  }
  .occPickerSection {
    width: 70%;
    height: 100%;
    background-color: #ffffff;
    float: right;
    position: relative;
    overflow: scroll;
  }
  .occPickerSectionHeader {
    padding: 0 15px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-bottom: 1px solid #9d9d9d;
    position: fixed;
  }
  .occPickerSectionHeader>span {
    font-size: 14px;
    color: #000000;
  }
  .searchDiv {
    width: 100%;
    overflow: hidden;
  }
</style>
